﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects.DataClasses;
using System.Data.Objects;

namespace Library
{
    public partial class AuthorBookView : Form
    {
        public Book book = new Book();
        LibraryEntities1 ins = new LibraryEntities1();
        public static ObjectSet<Author> authors;
        public static ObjectSet<BookAuthor> bookAuthors;
        

        public AuthorBookView(Book book)
        {
            InitializeComponent();
            this.book = book;
            authors = ins.Author;
            bookAuthors = ins.BookAuthor;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AuthorBookView_Load(object sender, EventArgs e)
        {
            bookView.Columns.Add("idAuthor", "idAuthor");
            bookView.Columns["idAuthor"].Visible = false;

            bookView.Columns.Add("Surname", "Фамилия");
            bookView.Columns["Surname"].Width = 150;
            bookView.Columns["Surname"].ReadOnly = true;

            bookView.Columns.Add("Name", "Имя");
            bookView.Columns["Name"].Width = 150;
            bookView.Columns["Name"].ReadOnly = true;

            bookView.Columns.Add("Patronymic", "Отчество");
            bookView.Columns["Patronymic"].Width = 147;
            bookView.Columns["Patronymic"].ReadOnly = true;

            
            int i = 0;
            foreach (BookAuthor x in book.BookAuthor)
            {
                bookView.Rows.Add();
                bookView.Rows[i].Cells[0].Value = x.Author.idAuthor;
                bookView.Rows[i].Cells[1].Value = x.Author.Surname;
                bookView.Rows[i].Cells[2].Value = x.Author.Name;
                bookView.Rows[i].Cells[3].Value = x.Author.Patronymic;
                i++;
            }
        }

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (bookView.CurrentRow != null)
            {

                int elementId = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                if (authors.Where(x => x.idAuthor.Equals(elementId)).Count() == 1)
                {
                    BookAuthor element = bookAuthors.Where(x => x.idAuthor.Equals(elementId)).Where(x=>x.bookid == book.bookid).FirstOrDefault();

                    bookAuthors.DeleteObject(element);
                    ins.SaveChanges();
                    bookView.Rows.Remove(bookView.CurrentRow);

                }
                else
                    bookView.Rows.Remove(bookView.CurrentRow);

            }
            else
            {
                MessageBox.Show("Выберите строку для удаления");
            }
        }

        private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AuthorsBook fm = new AuthorsBook(book);
            fm.ShowDialog();
            foreach (int xx in fm.selectedAuthors)
            {
                bookView.Rows.Add();
                Author author = authors.Where(x => x.idAuthor == xx).FirstOrDefault();
                bookView.Rows[bookView.Rows.Count - 1].Cells[0].Value = author.idAuthor;
                bookView.Rows[bookView.Rows.Count - 1].Cells[1].Value = author.Surname;
                bookView.Rows[bookView.Rows.Count - 1].Cells[2].Value = author.Name;
                bookView.Rows[bookView.Rows.Count - 1].Cells[3].Value = author.Patronymic;

                BookAuthor bookAuthor = new BookAuthor();
                bookAuthor.bookid = book.bookid;
                bookAuthor.idAuthor = author.idAuthor;
                ins.AddToBookAuthor(bookAuthor);
                ins.SaveChanges();
            }

        }
    }
}
